layui.use(['form'], function () {
    var form = layui.form;

    form.verify({
        parentName: function (value, item) {
            if (!value) {
                return "上级菜单为必填项";
            }
        }
        ,menuName: function (value, item) {
            if (!value) {
                return "菜单名称为必填项";
            }
        }
        ,path: function (value, item) {
            if (!value) {
                return "菜单Path为必填项";
            }
        }
        ,icon: function (value, item) {
            if (!value) {
                return "菜单图标为必填项";
            }
        }
    });

    //监听提交
    form.on('submit(addMenu)', function (data) {
        event.preventDefault();

        $.ajax({
            type: "POST",
            url: "/ams-admin/ams/menu/save",
            data: JSON.stringify(data.field),
            contentType: "application/json",
            success: function (r) {
                if(r['code'] === 0){
                    layer.alert("添加成功!", function () {
                        window.parent.location.reload();//刷新父页面
                        parent.layer.close(index);//关闭弹出层
                    });
                }else{
                    layer.alert(r['msg']);
                }
            }
        });
    });

    //表单取值
    layui.$('#LAY-component-form-getval').on('click', function () {
        var data = form.val('example');
        alert(JSON.stringify(data));
    });
});

function load_menuTree() {
    layer.open({
        type: 2
        ,title: "选择上级菜单"
        , closeBtn: 1
        , area: ['450px', '450px']
        , shade: 0.8
        , btn: ['确定', '取消']
        , btnAlign: 'c'
        , content: 'menu_tree.html'
    });
}